<?php
class TimediffHelper extends AppHelper {
	function timeDiff($t1, $t2)
	{
		//echo ('t2: '.$t2.' - t1 '.$t1.'<br>');
		$enddate = explode(" ",$t2);
		$startdate =  explode(" ",$t1);
		$dateEnd = explode("-", $enddate[0]);
		$hoursEnd=  explode(":", $enddate[1]);
		$dateStart = explode("-", $startdate[0]);
		$hoursStart=  explode(":", $startdate[1]);
		
		$tEnd = mktime($hoursEnd[0],$hoursEnd[1],$hoursEnd[2],$dateEnd[1],$dateEnd[2],$dateEnd[0]);
		$tStart = mktime($hoursStart[0],$hoursStart[1],$hoursStart[2],$dateStart[1],$dateStart[2],$dateStart[0]);
		$time_period = ( $tEnd - $tStart );
		
		$days = 0;
		$hours = 0;
		$minutes = 0;
		$seconds = 0;

		$time_increments = array( 'days' => 86400,
						'hrs' => 3600,
						'mins' => 60,
						'sec' => 1 );

		## will hold our values for ( day, minute, hour, seconds )
		$time_span = array();

		## cycle through time_increments
		while( list( $key, $value ) = each( $time_increments )) {
			$this_value = (int) ( $time_period / $value );
			$time_period = ( $time_period % $value );
				
			# save value
			$time_span[$key] = $this_value;
		}
		
		## show results
		while( list( $key, $value ) = each( $time_span )) {
					
			if($key == 'days' && $value!='0')
				print "$value $key and ";
			if($key == 'hours' && $value!='0')
				print "$value $key and ";
			if($key == 'mins')
				print "$value $key";


		}
	}
}
?>